package tcm.com.gistone.api.restController;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tcm.com.gistone.database.config.GetBySqlMapper;
import tcm.com.gistone.util.ClientUtil;
import tcm.com.gistone.util.EdatResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/10/20.
 */
@RestController("apiIndexController")
@RequestMapping("api/index")
public class IndexController {


    @Autowired
    private GetBySqlMapper getBySqlMapper;



    @Transactional
    @RequestMapping(value = "getUpVoteWordToImageList", method = RequestMethod.POST)
    public EdatResult getUpVoteWordToImageList(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }
            System.out.println(specialId);

            Map map =new HashMap();
            String sql = "select count(a.word) as no, a.special_id, " +
                    "  a.word,c.theme_name,b.pic_url  from tb_upvote_word a left JOIN tb_word b  on a.word =b.word LEFT JOIN tb_theme c" +
                    "    on c.theme_id =b.theme_type WHERE  a.status =2  ";
            if(null!=specialId){

                sql =sql+" and  a.special_id ="+specialId;
            }

            sql =sql +" GROUP BY  a.word, a.special_id,c.theme_name,b.pic_url  order by no desc;";


            List<Map> result = getBySqlMapper.findRecords(sql);


            System.out.println(result.size());

            return EdatResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }

    @Transactional
    @RequestMapping(value = "getUpVoteWordToSectionList", method = RequestMethod.POST)
    public EdatResult getUpVoteWordToSectionList(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }

            String sql = "select count(a.word) as no," +
                    "  a.word ,c.book_name,b.section_id,b.book_id,b.number,b.section_content,d.pic_url from tb_upvote_section a LEFT JOIN tb_section b on a.section=b.section_id " +
                    "  LEFT JOIN tb_bookinfo c ON  b.book_id =c.book_id" +
                    "   LEFT JOIN tb_word d ON  d.word =a.word"+
                    " WHERE  a.status =2 ";
            if(null!=specialId){

                sql =sql+" and  c.special_id ="+specialId;
            }


            sql =sql +" GROUP BY  a.word,c.book_name,b.section_id,b.book_id,b.number,b.section_content,d.pic_url  order by no desc;";


            System.out.println(sql);
            List<Map> result = getBySqlMapper.findRecords(sql);



            return EdatResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "发生错误");
        }
    }


    @Transactional
    @RequestMapping(value = "getStatic", method = RequestMethod.POST)
    public EdatResult getStatic(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));


            Map map =new HashMap();

            String sql = "select count(*)  from tb_bookInfo where status =7 ";
            int bookCount= getBySqlMapper.findrows(sql);

             sql = "select count(*)  from tb_section";

            int sectionCount= getBySqlMapper.findrows(sql);

            sql = "select count(distinct image_url ) from tb_directory where image_url is not null";

            int imageCount= getBySqlMapper.findrows(sql);


            sql = "select count(*) from tb_directory where image_url is not null";

            int imageSectionCount= getBySqlMapper.findrows(sql);

            sql = "select count(*) from tb_word";



            int keywordCount= getBySqlMapper.findrows(sql);


            sql = "select count( *) from tb_knowledge";

            int specialCount= getBySqlMapper.findrows(sql);




            map.put("bookCount",bookCount);
            map.put("sectionCount",sectionCount);
            map.put("imageCount",imageCount);//古 籍 原 图数 量
            map.put("imageSectionCount",imageSectionCount);//标 引 词数 量
            map.put("keywordCount",keywordCount);
            map.put("specialCount",specialCount);



            return EdatResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }





    /**
     * 古籍数量  （有多少种书）
     * specialId为空 取所有
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "getBookCount", method = RequestMethod.POST)
    public EdatResult getBookCount(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }
            String sql = "select count(*)  from tb_bookInfo";
            if(null!=specialId){

                sql =sql+" where special_id ="+specialId;
            }
            int bookCount= getBySqlMapper.findrows(sql);

            return EdatResult.ok(bookCount);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }

    /**
     * 结构化主题段落 （所有书原文excel表中段落数量）
     * 参数 bookId 为空 取所有
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "getBookSectionCount", method = RequestMethod.POST)
    public EdatResult getBookSectionCount(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer bookId = null;
            if (data.has("bookId")) {
                bookId = data.getInt("bookId");

            }
            String sql = "select count(*)  from tb_section ";
            if(null!=bookId){

                sql =sql+" where book_id ="+bookId;
            }
            int bookSectionCount= getBySqlMapper.findrows(sql);

            return EdatResult.ok(bookSectionCount);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }


    /**
     * 古籍原图数量  （有多少张古籍原图片）
     * specialId为空 取所有
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "getBookImageCount", method = RequestMethod.POST)
    public EdatResult getBookImageCount(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer bookId = null;
            if (data.has("bookId")) {
                bookId = data.getInt("bookId");

            }
            String sql = "select count(*)  from tb_section  where  image_Url is not null";
            if(null!=bookId){

                sql =sql+" and  book_id ="+bookId;
            }
            int bookImageCount= getBySqlMapper.findrows(sql);

            return EdatResult.ok(bookImageCount);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }

    /**
     * 专家提取知识  （专家提取表里词的总量，可不分专题）
     * bookId 为空 取所有
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(value = "getKnowledgeCount", method = RequestMethod.POST)
    public EdatResult getKnowledgeCount(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            Integer specialId = null;
            if (data.has("specialId")) {
                specialId = data.getInt("specialId");

            }
            String sql = "select count(*)  from tb_knowledge ";
            if(null!=specialId){

                sql =sql+" where special_id ="+specialId;
            }

            int KnowledgeCount= getBySqlMapper.findrows(sql);

            return EdatResult.ok(KnowledgeCount);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "fail");
        }
    }



}
